import VueRouter from "vue-router";
import Vue from 'vue'
import {SHOWCURPLAYING} from "@/http/constant";
import {isLoggedIn} from "@/http/tools";
import store from "@/store";

const mainPage = () => import('@/pages/mainPage.vue')
const songListPage = () => import('@/pages/songListDetailPage.vue')
const albumDetailPage = () => import('@/pages/AlbumListDetailPage.vue')
const singerDetailPage = () => import('@/pages/SingerListDetailPage.vue')
const findPage = () => import('@/pages/FindPage.vue')
const mvDetailsPage = () => import('@/pages/MvDetailsPage.vue')
const totalSingersPage = () => import('@/pages/TotalSingerListPage.vue')
const totalMvsPage = () => import('@/pages/AllMvsPage.vue')
const router = new VueRouter({
    // base: '/nova_music/',
    routes: [
        {
            path: '/',
            // redirect: '/albumDetail'
            redirect: '/mainPage'
            // redirect: '/singersPage'
        },
        {
            name: 'main-page',
            path: '/mainPage',
            component: mainPage
        },
        {
            name: 'song-detail',
            // 传递了params参数
            path: '/songDetail/:id',
            component: songListPage
        },
        {
            name: 'album-detail',
            path: '/albumDetail/:id',
            component: albumDetailPage,
            props: true
        },
        {
            name: 'singer-detail',
            path: '/singerDetail/:id',
            component: singerDetailPage,
            // 将params参数以Props的形式传递给组件
            props: true
        },
        {
            name: 'find-page',
            path: '/findPage',
            component: findPage,
            props: true,
        },
        {
            name: 'mv-detail',
            path: '/mvPages/:id',
            component: mvDetailsPage,
            props: true
        },
        {
            name: 'total-singers',
            path: '/singersPage',
            component: totalSingersPage
        },
        {
            name: 'total-mvs',
            path: '/singerMvs/:singerId/:singerName',
            component: totalMvsPage,
            props: true
        }
    ]
})
router.afterEach((to, from) => {
    // 当路由跳转的时候先隐藏正在播放列表页面
    Vue.prototype.$bus.$emit(SHOWCURPLAYING, {
        isUseFlag: true,
        flag: false
    })
})
router.beforeEach((to, from, next) => {
    // 如果之前已经登陆过有cookie就请求数据
    if (isLoggedIn()) {
        store.dispatch('fetchUserProfile')
    }
    next()
})
export default router